﻿using LD.ZKT;
using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace LD
{
    public partial class FrmRegister : Form
    {
        private UserHelper user = UserHelper.Instance;
        private string TouchId = string.Empty;
        public FrmRegister()
        {
            InitializeComponent();
            ZKTHelper.Instance.ImageChangedEvent += Instance_ImageChangedEvent;
            ZKTHelper.Instance.MessageChangeEvent += Instance_MessageChangeEvent;
        }

        private void Instance_MessageChangeEvent(string message)
        {
            if (!this.IsDisposed)
            {
                this.Invoke(new Action(() =>
                {
                    this.lab_text.Text = message;

                    if (message == "注册成功")
                    {
                        TouchId = ZKTHelper.Instance.TouchId;
                    }
                }));
            }
        }

        private void Instance_ImageChangedEvent(Bitmap bitmap)
        {
            picFPImg.Image = bitmap;
        }

        private void FrmUser_Load(object sender, EventArgs e)
        {
            CreateUI();
        }

        private void btn_enabled_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txt_userName.Text) ||
                string.IsNullOrEmpty(this.txt_pwd1.Text) ||
                string.IsNullOrEmpty(this.txt_pwd2.Text))
                return;
            if (this.txt_pwd2.Text != this.txt_pwd2.Text)
            {
                MessageBox.Show("2次录入密码不一致", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            var isExistUser = UserHelper.Instance.GetUsers.Where(p => p.Name == this.txt_userName.Text).FirstOrDefault();
            if (isExistUser != null)
            {
                MessageBox.Show("用户已存在", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            //指纹采集
            if (string.IsNullOrEmpty(TouchId))
            {
                DialogResult result = MessageBox.Show("未录入指纹信息,还继续吗?", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                if (result == DialogResult.Cancel)
                    return;
            }
            var _user = new UserModel()
            {
                Name = this.txt_userName.Text,
                Pwd = this.txt_pwd2.Text,
                UserType = (UserType)Enum.Parse(typeof(UserType), cob_userType.Text),
                TouchId = TouchId,
                Id = user.GetUsers.Count + 1
            };

            UserHelper.Instance.Add(_user);
            if (!string.IsNullOrEmpty(TouchId))
            {
                TB_UserTouch userTouch = new TB_UserTouch()
                {
                    Role = (int)(UserType)Enum.Parse(typeof(UserType), cob_userType.Text),
                    TouchId = TouchId,
                    UserId = _user.Id,
                };
                DBHelper.Instance.Sql.Insertable(userTouch).ExecuteCommand();
            }
            this.txt_pwd1.Text = string.Empty;
            this.txt_pwd2.Text = string.Empty;
            this.txt_userName.Text = string.Empty;
            TouchId = string.Empty;
            LogHelper.Instance.ParamChange($"【注册用户】{_user.Name}");

            MessageBox.Show("保存成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        private void btn_canel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void CreateUI()
        {
            foreach (var item in Enum.GetValues(typeof(UserType)))
            {
                this.cob_userType.Items.Add(item.ToString());
            }
        }

        private void btn_touchId_Click(object sender, EventArgs e)
        {
            TouchId = string.Empty;
            int id = DBHelper.Instance.Sql.Queryable<TB_UserTouch>().Max(p => p.Id) + 1;
            ZKTHelper.Instance.Fid = id;
            ZKTHelper.Instance.Enroll();
        }

        private void FrmRegister_FormClosing(object sender, FormClosingEventArgs e)
        {
            ZKTHelper.Instance.ImageChangedEvent -= Instance_ImageChangedEvent;
            ZKTHelper.Instance.MessageChangeEvent -= Instance_MessageChangeEvent;
        }
    }
}
